﻿using System.Drawing;
using System.Windows.Forms;
using FBEntities.FBObjects.User;
using XDAFacebook;
using XFControls.Utils;
using FBEntities.FBObjects;

namespace XFControls.XFPanels.XFPanelItems
{
    internal class XFItemProfileInfo : XFItemBase
    {
        private int _categoryHeight = 30;
        private int _height;
        private int _indent = 15;
        private Size _mesTitle;
        private int _singleLineText = 30;
        private FBPerson _user;

        private Rectangle _profile;
        private Rectangle _email;
        private Rectangle _gender;
        private Rectangle _birthday;
        private Rectangle _hometown;
        private Rectangle _relationship;
        private Rectangle _likes;
        private Rectangle _activities;
        private Rectangle _intrests;
        private Rectangle _music;
        private Rectangle _tv;
        private Rectangle _movies;
        private Rectangle _books;
        private Rectangle _quotes;
        private Rectangle _bio;
        private UpdateEventHandler _userUpdateEvent;

        public XFItemProfileInfo()
        {
            _height = -1;
            _mesTitle = new Size(-1, -1);

            Style = new XFItemStyle
                        {
                            BackgroundColor = Color.White,
                            TextFont = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular),
                            TextColor = Color.DarkSlateGray,
                            Padding = 10
                        };

            if (XFGraphics.GetScreenResolutionNotRotated() == ScreenResolution.QVGA)
            {
                _categoryHeight /= 2;
                _singleLineText /= 2;
                _indent /= 2;
            }
            ItemType = XFItemType.Display;
        }

        public FBPerson User
        {
            get { return _user; }
            set
            {
                if (_userUpdateEvent == null)
                    _userUpdateEvent = (o, e) =>
                                     {
                                         ResetHeight();
                                         GetHeight();
                                         Invalidate(false);
                                         if (Parent != null && !Parent.IsDisposed)
                                             Parent.Damage();
                                     };

                if (_user != null)
                    _user.Updated -= _userUpdateEvent;

                _user = value;
                _user.Updated += _userUpdateEvent;

            }
        }

        public override int GetHeight()
        {
            if (_height > 0)
                return _height;

            int width = Screen.PrimaryScreen.WorkingArea.Width;
            int curY = Style.PaddingTop;

            determinLongest();

            curY = getCategoryHeight(curY);

            curY = getLineTextHeight(curY);
            curY = getLineTextHeight(curY);
            curY = getLineTextHeight(curY);
            curY = getLineTextHeight(curY);
            curY = getLineTextHeight(curY);
            curY = getLineTextHeight(curY);

            curY = getCategoryHeight(curY);

            curY = getParagraphTextHeight(width, User.Activities.ToString(), curY, ref _activities);
            curY = getParagraphTextHeight(width, User.Interests.ToString(), curY, ref _intrests);
            curY = getParagraphTextHeight(width, User.Music.ToString(), curY, ref _music);
            curY = getParagraphTextHeight(width, User.Television.ToString(), curY, ref _tv);
            curY = getParagraphTextHeight(width, User.Movies.ToString(), curY, ref _movies);
            curY = getParagraphTextHeight(width, User.Books.ToString(), curY, ref _books);
            curY = getParagraphTextHeight(width, User.quotes, curY, ref _quotes);
            curY = getParagraphTextHeight(width, User.bio, curY, ref _bio);

            _height = curY;

            return _height;
        }

        public override void ResetHeight()
        {
            _height = -1;
        }

        public override void ItemPaint(Graphics gf, int x, int y)
        {
            base.ItemPaint(gf, x, y);
            int width = GetWidth();
            int xLocal = 0;

            int curY = 0;
            using (var pCat = new Pen(Color.MidnightBlue))
            using (var bCat = new SolidBrush(Color.LightSteelBlue))
            using (var fCat = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular))
            using (var bText = new SolidBrush(Color.Black))
            {
                curY = drawCategory(gf, width, Settings.LocalizationStrings.basicInfo, pCat, bCat, bText, fCat, xLocal, curY);
                curY = drawLineText(gf, width, Settings.LocalizationStrings.Profile + ":  " + User.link, bText, curY);
                curY = drawLineText(gf, width, Settings.LocalizationStrings.Email + ":  " + User.email, bText, curY);
                curY = drawLineText(gf, width, Settings.LocalizationStrings.gender + ":  " + User.gender, bText, curY);
                curY = drawLineText(gf, width, Settings.LocalizationStrings.Birthday + ":  " + User.birthday, bText, curY);
                curY = User.location != null ? drawLineText(gf, width, Settings.LocalizationStrings.Hometown + ":  " + User.location.name, bText, curY) : drawLineText(gf, width, Settings.LocalizationStrings.Hometown + ":  N/A", bText, curY);
                curY = drawLineText(gf, width, Settings.LocalizationStrings.Relationship + ":  " + User.relationship_status, bText, curY);
                drawCategory(gf, width, Settings.LocalizationStrings.likesAndInterests, pCat, bCat, bText, fCat, xLocal, curY);
                drawParagraphText(gf, User.Activities.ToString(), Settings.LocalizationStrings.Activities, bText, _activities);
                drawParagraphText(gf, User.Interests.ToString(), Settings.LocalizationStrings.Interests, bText, _intrests);
                drawParagraphText(gf, User.Music.ToString(), Settings.LocalizationStrings.Music, bText, _music);
                drawParagraphText(gf, User.Television.ToString(), Settings.LocalizationStrings.TV, bText, _tv);
                drawParagraphText(gf, User.Movies.ToString(), Settings.LocalizationStrings.Movies, bText, _movies);
                drawParagraphText(gf, User.Books.ToString(), Settings.LocalizationStrings.Books, bText, _books);
                drawParagraphText(gf, User.quotes, Settings.LocalizationStrings.Quotes, bText, _quotes);
                drawParagraphText(gf, User.bio, Settings.LocalizationStrings.Bio, bText, _bio);
            }
        }

        private void determinLongest()
        {
            if (_mesTitle.Width > 0)
                return;

            string cur;
            Size curMes;

            cur = Settings.LocalizationStrings.Activities;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;
            cur = Settings.LocalizationStrings.Interests;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;
            cur = Settings.LocalizationStrings.Music;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;
            cur = Settings.LocalizationStrings.TV;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;
            cur = Settings.LocalizationStrings.Movies;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;
            cur = Settings.LocalizationStrings.Books;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;
            cur = Settings.LocalizationStrings.Quotes;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;
            cur = Settings.LocalizationStrings.Bio;
            curMes = Style.TextFont.MeasureString(cur, new Rectangle(0, 0, 400, 0));
            if (_mesTitle.Width < curMes.Width)
                _mesTitle = curMes;

            _mesTitle.Width += _indent;
        }

        private int drawLineText(Graphics g, int width, string text, SolidBrush bText, int curY)
        {
            if (Style == null) return curY + _singleLineText;
            g.DrawString(XFControlUtils.EllipsisWord(width - (_indent * 2), text, Style.TextFont), Style.TextFont, bText,
                         _indent, curY);
            return curY + _singleLineText;
        }

        private int getLineTextHeight(int curY)
        {
            return curY + _singleLineText;
        }

        private void drawParagraphText(Graphics g, string text, string title, SolidBrush bText, Rectangle rec)
        {
            title = title + ":  ";
            if (rec.Y <= 0) return;

            g.DrawString(title, Style.TextFont, bText, _indent, rec.Y);
            g.DrawString(text, rec, Style, bText.Color);
        }

        private int getParagraphTextHeight(int width, string text, int curY, ref Rectangle output)
        {
            int titleWidth = _mesTitle.Width;
            output = new Rectangle(_indent + titleWidth, curY, width - (_indent * 2) - titleWidth, 0);

            if (string.IsNullOrEmpty(text))
                return curY + _singleLineText;

            int recHeight = Style.TextFont.MeasureString(text, output).Height;
            output.Height = recHeight;

            return curY + recHeight + Style.PaddingTop;
        }

        private int drawCategory(Graphics g, int width, string title, Pen pCat, SolidBrush bCat, SolidBrush bText,
                                 Font fCat, int x, int curY)
        {
            int ret = curY + _categoryHeight + Style.PaddingTop;

            g.FillRectangle(bCat, x, curY, width, _categoryHeight);
            g.DrawLine(pCat, x, curY, width, curY);

            g.DrawString(title, fCat, bText, x + Style.PaddingLeft, curY + 3);

            return ret;
        }

        private int getCategoryHeight(int curY)
        {
            return curY + _categoryHeight + (Style.PaddingLeft + Style.PaddingRight);
        }

        public override void Dispose()
        {
            base.Dispose();
            _user.Updated -= _userUpdateEvent;
        }
    }
}